package com.springclouddemo.consumer.service;

import com.springclouddemo.demoapi.domain.Test;
import com.springclouddemo.demoapi.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Collection;
@Service
public class ConsumerService implements TestService {

    private static final String PROVIDER_SERVER_URL_PREFIX = "http://service-provider";
    /**
     * 通过 REST API 代理到服务器提供者
     */
    @Autowired
    private RestTemplate restTemplate;


    @Override
    public boolean save(Test test) {
        Test returnValue =
                restTemplate.postForObject(PROVIDER_SERVER_URL_PREFIX + "/save", test, Test.class);
        return returnValue != null;

    }

    @Override
    public Collection<Test> findall() {
        Collection<Test> collection = restTemplate.getForObject(PROVIDER_SERVER_URL_PREFIX + "/list", Collection.class);
        return collection;
    }
}
